package com.house365.im.client.util;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.Context;
import android.text.TextUtils;

public class ActivityUtil {



	public static String getTopActivity(Context context) {
		ActivityManager manager = (ActivityManager) context
				.getSystemService(Context.ACTIVITY_SERVICE);
		List<RunningTaskInfo> runningTaskInfos = manager.getRunningTasks(1);

		if (runningTaskInfos != null) {
			return (runningTaskInfos.get(0).topActivity).getClassName();
		} else {
			return null;
		}
	}
	public static String getBaseActivity(Context context) {
		ActivityManager manager = (ActivityManager) context
				.getSystemService(Context.ACTIVITY_SERVICE);
		List<RunningTaskInfo> runningTaskInfos = manager.getRunningTasks(1);

		if (runningTaskInfos != null) {
			return (runningTaskInfos.get(0).baseActivity).getClassName();
		} else {
			return null;
		}
	}
	public static boolean isTopActivity(Activity context) {
		String s = getTopActivity(context);
		if (s == null) {
			return false;
		}
		String s1 = context.getClass().getName();
		return s1.equals(s);
	}
	public static boolean isAppOnForeground(Context context,String baseActivtyName)
    {
		String s = getBaseActivity(context);
		if (s == null) {
			return false;
		}
		return baseActivtyName.equals(s);
    }

	public static boolean isServiceRunning(Context mContext, String className) {
		boolean isRunning = false;
		ActivityManager activityManager = (ActivityManager) mContext
				.getSystemService(Context.ACTIVITY_SERVICE);
		List<ActivityManager.RunningServiceInfo> serviceList = activityManager
				.getRunningServices(Integer.MAX_VALUE);
		if (serviceList == null || (serviceList.size() == 0)) {
			return false;
		}
		for (int i = 0; i < serviceList.size(); i++) {
			if (serviceList.get(i).service.getClassName().equals(className)) {
				isRunning = true;
				break;
			}
		}
		return isRunning;
	}
	
	public static final boolean isDidiMsg(String msg) {
		if (TextUtils.isEmpty(msg)) {
			return false;
		}
		String PATTERN = "\\[(.+?)嘀嘀\\]";
		Pattern p = Pattern.compile(PATTERN);
		Matcher matcher = p.matcher(msg);
		if (matcher.find()) {
			return true;
		}
		
		return false;
	}
}
